package com.xidian.zhiancloud.controller.chemicalsManagement.wastePart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.WasteWarehouseOut;
import com.xidian.zhiancloud.qo.chemicalsManagement.WasteWarehouseOutQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart.WasteWarehouseOutService;
import com.xidian.zhiancloud.vo.chemicalsManagement.WasteWarehouseOutVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * 危化品管理 废弃物库存管理 废弃物出库
 * @Author: Egoist
 * @Date: 2020/11/10 17:31
 */
@RestController
@RequestMapping("/WasteWarehouseOut")
public class WasteWarehouseOutController extends BaseController {

    @Autowired
    private WasteWarehouseOutService wasteWarehouseOutService;

    /**
     * 增加废弃物出库记录
     *
     * @param wasteWarehouseOut 废弃物出库对象
     * @return 返回是否添加成功
     */
    @Transactional(rollbackFor = {RuntimeException.class, Error.class, CustomizeException.class})
    @PostMapping("/add")
    public ResultDTO<Boolean> add(WasteWarehouseOut wasteWarehouseOut) {
        wasteWarehouseOut.setCompanyId(getSessionUserCompanyId());
        wasteWarehouseOut.setOuterId(getSessionUserid());
        if (wasteWarehouseOut.getOutTime() == null) wasteWarehouseOut.setOutTime(new Date(System.currentTimeMillis()));
        return wasteWarehouseOutService.add(wasteWarehouseOut);
    }

    /**
     * 根据id获取废弃物出库记录
     *
     * @param id 废弃物出库记录id
     * @return 废弃物出库对象
     */
    @GetMapping("/getById")
    public ResultDTO<WasteWarehouseOutVO> getById(Long id) {
        return ResultDTO.successOf(wasteWarehouseOutService.getVOById(id), GeneralResponseEnums.SUCCESS);
    }


    /**
     * 列表查询
     *
     * @param wasteWarehouseOutQO 请求参数
     * @return 返回废弃物出库列表
     */
    @GetMapping("/getListByQueryObject")
    public ResultDTO<IPage<WasteWarehouseOutVO>> getListByQueryObject(WasteWarehouseOutQO wasteWarehouseOutQO){
        if (wasteWarehouseOutQO.getCurrent() == null || wasteWarehouseOutQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        wasteWarehouseOutQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(wasteWarehouseOutService.getListByQueryObject(wasteWarehouseOutQO), GeneralResponseEnums.SUCCESS);
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, Long[] ids) throws IOException {
        ResultDTO.successOf(wasteWarehouseOutService.export(response, ids), GeneralResponseEnums.SUCCESS);
    }
}
